<?php

declare(strict_types=1);

namespace Plugin\Attractions\AttractionManagement\Controller;

use Common\Common;
use Common\CommonController;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Swagger\Annotation\HyperfServer;
use Plugin\Attractions\AttractionManagement\Aspect\CheckJwtToken;
use Plugin\Attractions\AttractionManagement\Repository\AttractionScoreRepository;

#[HyperfServer(name: 'http')]
#[Controller('attractions/attraction-management')]
class ScoreController extends CommonController
{ 
    #[Inject]
    private AttractionScoreRepository $repository;

    #[PostMapping('addScore'), CheckJwtToken]
    public function addScore()
    {
        $data = $this->valid([
            'spotId' => 'required|integer',
            'score' => 'required|integer|in:1,2,3,4,5',
            'remark' => 'string'
        ], []);
        $userId = Common::parseJwt(
            $this->getRequest()->getHeaderLine('Authorization')
        )['id'];
        $data['user_id'] = $userId;
        $data['spot_id'] = $data['spotId'];
        return $this->success($this->repository->create($data));
    }

    #[PostMapping('updateScore'), CheckJwtToken]
    public function updateScore()
    {
        $data = $this->valid([
            'id' => 'required|integer',
            'score' => 'integer|in:1,2,3,4,5',
            'remark' => 'string'
        ], []);
        $userId = Common::parseJwt(
            $this->getRequest()->getHeaderLine('Authorization')
        )['id'];
        $data['userId'] = $userId;
        $id = $data['id'];
        unset($data['id']);
        return $this->success($this->repository->updateById($id, $data));
    }

}